package com.sptpc.androidclass1021;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
    MyBroadcastReceiver myBroadcastReceiver;
    Button broadBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Teacher teacher = new Teacher(MainActivity.this);
;
        Student stu1 = new Student(MainActivity.this,new BroadcastReceiver(){
            @Override
            public void onReceive(Context context, Intent intent) {

                Toast.makeText(MainActivity.this,"学生1收到",Toast.LENGTH_SHORT).show();
                Log.d("学生","student1 getBroadCast");
            }
        });
        Student stu2 = new Student(MainActivity.this,new BroadcastReceiver(){
            @Override
            public void onReceive(Context context, Intent intent) {
                Toast.makeText(MainActivity.this,"学生2收到",Toast.LENGTH_SHORT).show();
                Log.d("学生","student2 getBroadCast");
            }
        });
//        registerBroadCastReceiver(stu1);
//        stu1.onReceive(MainActivity.this,intent1);
        broadBtn = findViewById(R.id.btn1);
        broadBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Intent intent = new Intent("BC_ACTION");
//                sendBroadcast(intent);
                teacher.sendBroadcast();
            }
        });
    }
//    private void registerBroadCastReceiver() {
//        IntentFilter filter = new IntentFilter();
//        filter.addAction("BC_ACTION");
//        myBroadcastReceiver = new MyBroadcastReceiver();
//        registerReceiver(myBroadcastReceiver,filter);
//        //注册
//    }
    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(myBroadcastReceiver);
    }
}
